﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Sworm
{
    static class Helpers
    {
        private static BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

        public static IEnumerable<Member> SelectWritableMembers(this Type t)
        {
            return t.GetProperties(flags).Select(p => new Member(p))
                .Concat(t.GetFields(flags).Select(f => new Member(f)))
                .Where(m => m.IsPubliclyWritable);
        }

        public static Type UndoEnumerable(Type type)
        {
            var iface = type.GetInterface(typeof(IEnumerable<>).Name);
            if (iface != null)
            {
                return iface.GetGenericArguments().Single();
            }
            return type;
        }

        public static bool EqualsIgnoreCase(this string first, string second)
        {
            if (first == null || second == null)
            {
                return false;
            }
            return first.Equals(second, StringComparison.OrdinalIgnoreCase);
        }
    }
}
